package com.cluster.platform.service.impl;

import com.cluster.platform.common.api.CommonPage;
import com.cluster.platform.common.api.CommonResult;
import com.cluster.platform.common.api.ResultCode;
import com.cluster.platform.mapper.CmsCommentsInfoMapper;
import com.cluster.platform.model.CmsCommentsInfo;
import com.cluster.platform.service.CmsCommentsInfoService;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @ClassName: CmsCommentsInfoServiceImpl
 */
@Service
public class CmsCommentsInfoServiceImpl implements CmsCommentsInfoService {

    @Autowired
    private CmsCommentsInfoMapper cmsCommentsInfoMapper;

    /**
     * 新增评论信息
     *
     * @param cmsCommentsInfo
     */
    @Override
    @Transactional
    public CommonResult<CmsCommentsInfo> addComments(CmsCommentsInfo cmsCommentsInfo) {
        if (null == cmsCommentsInfo) {
            return CommonResult.failed("未获取传入信息");
        }
        cmsCommentsInfo.setId(String.valueOf(System.currentTimeMillis()));
        cmsCommentsInfo.setInsertDate(new Date());
        cmsCommentsInfo.setStatus(1);
        int insertSelective = cmsCommentsInfoMapper.insertSelective(cmsCommentsInfo);
        if (insertSelective < 0) {
            return CommonResult.failed("新增评论失败");
        }
        return CommonResult.success(cmsCommentsInfo);
    }

    /**
     * 删除评论-逻辑
     *
     * @param id
     */
    @Override
    @Transactional
    public CommonResult<Object> deleteComments(String id) {
        if (StringUtils.isBlank(id)) {
            return CommonResult.failed("未获取到删除标志");
        }
        CmsCommentsInfo cmsCommentsInfo = new CmsCommentsInfo();
        cmsCommentsInfo.setId(id);
        cmsCommentsInfo.setUpdateDate(new Date());
        cmsCommentsInfo.setStatus(0);
        int update = cmsCommentsInfoMapper.updateByPrimaryKeySelective(cmsCommentsInfo);
        if (update < 0) {
            return CommonResult.failed("删除失败");
        }
        return CommonResult.success(id);
    }

    /**
     * 批量删除评论
     *
     * @param cmsCommentsInfos
     */
    @Override
    @Transactional
    public CommonResult<Object> batchDeleteComments(List<CmsCommentsInfo> cmsCommentsInfos) {
        if (null == cmsCommentsInfos || cmsCommentsInfos.isEmpty()) {
            return CommonResult.failed("未获取到参数信息");
        }
        int update = cmsCommentsInfoMapper.batchUpdateByStatus(cmsCommentsInfos);
        if (update<0) {
            return CommonResult.failed("批量删除失败");
        }
        return CommonResult.success(update);
    }

    /**
     * 查询所有评论信息
     *
     * @param cmsCommentsInfo
     */
    @Override
    public CommonResult<Object> searchComments(CmsCommentsInfo cmsCommentsInfo) {
        List<CmsCommentsInfo> cmsCommentsInfos = cmsCommentsInfoMapper.searchByExampleSelective(cmsCommentsInfo);
        if (null == cmsCommentsInfos || cmsCommentsInfos.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(cmsCommentsInfos);
    }

    /**
     * 分页查询所有评论信息
     *
     * @param cmsCommentsInfo
     * @param pageNum
     * @param pageSize
     */
    @Override
    public CommonResult<Object> searchCommentsPage(CmsCommentsInfo cmsCommentsInfo, Integer pageNum, Integer pageSize) {
        if (null == pageNum || null == pageSize) {
            return CommonResult.failed("未获取到分页参数");
        }
        PageHelper.startPage(pageNum, pageSize);
        List<CmsCommentsInfo> cmsCommentsInfos = cmsCommentsInfoMapper.searchByExampleSelective(cmsCommentsInfo);
        if (null == cmsCommentsInfos || cmsCommentsInfos.isEmpty()) {
            return CommonResult.failed("未查询到信息");
        }
        return CommonResult.success(CommonPage.restPage(cmsCommentsInfos));
    }

    @Override
    public CommonResult<Object> searchCommentsByTopicId(String topicId) {
        if (StringUtils.isBlank(topicId)) {
            return CommonResult.failed(ResultCode.PARAM_NULL);
        }
        List<CmsCommentsInfo> cmsCommentsInfos = cmsCommentsInfoMapper.searchCommentsByTopicId(topicId);
        if (cmsCommentsInfos.isEmpty()) {
            return CommonResult.success(ResultCode.NO_RECORD);
        }
        return CommonResult.success(cmsCommentsInfos);
    }
}
